package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WebRecycleType;
import com.ruoyi.system.domain.WebUser;
import com.ruoyi.system.domain.WebUserAccount;
import com.ruoyi.system.service.IWebRecycleTypeService;
import com.ruoyi.system.service.IWebUserAccountService;
import com.ruoyi.system.service.IWebUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2021-05-22
 */
@Controller
@RequestMapping("/system/webUser")
public class WebUserController extends BaseController
{
    private String prefix = "system/webUser";

    @Autowired
    private IWebUserService webUserService;
    @Autowired
    private IWebUserAccountService iWebUserAccountService;

    @RequiresPermissions("system:webUser:view")
    @GetMapping()
    public String webUser(/*@RequestParam(defaultValue = "1") Integer userType ,*/String buttonType , ModelMap mmap)
    {
//        mmap.put("userType",userType);
        mmap.put("buttonType",buttonType);
        return prefix + "/webUser";
    }

    /**
     * 查询用户列表
     */
    @RequiresPermissions("system:webUser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebUser webUser)
    {
        startPage();
        List<WebUser> list = webUserService.selectWebUserList(webUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("system:webUser:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebUser webUser)
    {
        List<WebUser> list = webUserService.selectWebUserList(webUser);
        ExcelUtil<WebUser> util = new ExcelUtil<WebUser>(WebUser.class);
        return util.exportExcel(list, "用户数据");
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("system:webUser:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebUser webUser)
    {
        return toAjax(webUserService.insertWebUser(webUser));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WebUser webUser = null;
        if(id == null || id ==  -1){
            webUser = new WebUser();
        }else{
            webUser = webUserService.selectWebUserById(id);
        }

        WebUserAccount account = iWebUserAccountService.getOne(new QueryWrapper<WebUserAccount>().eq("user_id",webUser.getId()));
        if(account == null){
            account = new WebUserAccount();
        }
        WebRecycleType webRecycleType = new WebRecycleType();
        webRecycleType.setType(1);
        webRecycleType.setParentId(0L);
        List<WebRecycleType> listRecycleType = iwebRecycleTypeService.selectWebRecycleTypeList(webRecycleType);
        mmap.put("listRecycleType",listRecycleType);
        mmap.put("webUser", webUser);
        mmap.put("account", account);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("system:webUser:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebUser webUser , WebUserAccount account)
    {
        return toAjax(webUserService.updateWebUser(webUser, account));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("system:webUser:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(webUserService.deleteWebUserByIds(ids));
    }


    @Autowired
    private IWebRecycleTypeService iwebRecycleTypeService;
}
